package com.geuso.xingdutch.numbers;

import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.geuso.xingdutch.R;
import com.geuso.xingdutch.util.SoundUtil;

public class NumbersExerciseActivity extends Activity {
	
	private static int current_number_index;
	/*private static int min_number;
	private static int max_number;*/
	
	
	private TextView number_view;
	private EditText number_guess;
	private TextView guess_feedback;
	
	private Random rnd;
	
	private static String[] rightAnswersFeedback = {
		"Smart baby", 
		"A yao, kiss kiss",
		"我超级爱你",
		"Smart baby", 
		"我草集爱你",
		"Smart baby", 
		"Ik hou van jou!",
		"Love you",
		"Smart bao bao"
	};
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
        getActionBar().setDisplayHomeAsUpEnabled(true);
		setContentView(R.layout.numbers_exercise);

		current_number_index = 0;
		
		number_view = (TextView) findViewById(R.id.ne_num);
		number_guess = (EditText) findViewById(R.id.ne_dutch_guess);
		guess_feedback = (TextView) findViewById(R.id.ne_guess_feedback);
		

		number_guess.setEnabled(true);
		findViewById(R.id.ne_btn_submit).setEnabled(true);
		
		rnd = new Random();
		refreshDisplayedNumber();
		
		number_guess.setOnEditorActionListener(new OnEditorActionListener(){

			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_SEARCH ||
	                    actionId == EditorInfo.IME_ACTION_DONE ||
	                    event.getAction() == KeyEvent.ACTION_DOWN &&
	                    event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
		            submit(findViewById(R.id.ne_btn_submit));
		            return true;
		        }
		        else {
		            return false;
		        }
			}
		});
		
	}
	
	
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
        	Intent intent = NavUtils.getParentActivityIntent(this); 
    		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    		NavUtils.navigateUpTo(this, intent);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
	
	
	private void refreshDisplayedNumber(){
		guess_feedback.setText("");
		guess_feedback.setVisibility(View.INVISIBLE);
		
		DutchNumber new_number = NumbersData.numbers.get(current_number_index);
		if (new_number != null){
			number_view.setText(new_number.getNumber_id() + "");
		}
		else {
			Log.d(this.getClass().getName(), "Obtained number does not exist for index: " + current_number_index);
		}
		
		number_guess.setText("");
		number_guess.setEnabled(true);
		
		findViewById(R.id.ne_btn_submit).setEnabled(true);
	}
	
	
	public void previousNumber(View v){
		if (current_number_index <= 0){
			current_number_index = NumbersData.numbers.size()-1;
		}
		else {
			current_number_index--;
		}
		refreshDisplayedNumber();
	}
	
	public void randomNumber(View v){
		current_number_index = rnd.nextInt(NumbersData.numbers.size());
		Log.d(this.getClass().getName(), "Generated new number: " + current_number_index);
		refreshDisplayedNumber();
		
	}
	
	public void nextNumber(View v){
		if (current_number_index >= NumbersData.numbers.size()-1){
			current_number_index = 0;
		}
		else {
			current_number_index++;
		}
		refreshDisplayedNumber();
	}
	
	
	public void showAnswer(View v){
		DutchNumber number = NumbersData.numbers.get(current_number_index);
		guess_feedback.setText("Right answer: '" + number.getDutch() + "'");
		guess_feedback.setVisibility(View.VISIBLE);
		guess_feedback.setTextAppearance(getApplicationContext(), R.style.AnswerFeedback);
		
		SoundUtil.playSound(getApplicationContext(), number.getSound_file());
		
		number_guess.setEnabled(false);
		findViewById(R.id.ne_btn_submit).setEnabled(false);
	}
	
	public void submit(View v){
		String entered = number_guess.getText().toString();
		
		Log.d(this.getClass().getName(), "entered string: '"+ entered +"'");
		Log.d(this.getClass().getName(), "expected string: '"+ 
				NumbersData.numbers.get(current_number_index).getDutch() +"'");
		if (!entered.trim().equals("")){
			checkGuessAndSetFeedback(entered, NumbersData.numbers.get(current_number_index));
		}
		else {
			setAnswerFeedback("Write the number first!");
		}
		
	}
	
	private void checkGuessAndSetFeedback(String entered, DutchNumber expected){
		guess_feedback.setVisibility(View.VISIBLE);
		if (entered.trim().equals(expected.getDutch())){
			guess_feedback.setTextAppearance(getApplicationContext(), R.style.AnswerRight);
			guess_feedback.setText(rightAnswersFeedback[rnd.nextInt(rightAnswersFeedback.length)]);
			SoundUtil.playSound(getApplicationContext(), R.raw.flair_kisskiss);
			
		}
		else if (entered.trim().equals(expected.getNumber_id()+"")){
			setAnswerFeedback("Cheater!! Smart bao bao");
			SoundUtil.playSound(getApplicationContext(), R.raw.flair_cheater);
		}
		else {
			guess_feedback.setTextAppearance(getApplicationContext(), R.style.AnswerWrong);
			guess_feedback.setText(getResources().getString(R.string.ne_guess_wrong_pre));
		}
		
	}
	
	private void setAnswerFeedback(String feedback){
		guess_feedback.setText(feedback);
		guess_feedback.setTextAppearance(getApplicationContext(), R.style.AnswerFeedback);
		guess_feedback.setVisibility(View.VISIBLE);
	}
	
}
